package proxy.CGLIBProxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import proxy.staticProxy.TestFunctionSpendTime;

import java.lang.reflect.Method;

/**
 * @author wmin
 * @param <T>
 */
public class TenantInterceptor<T> implements MethodInterceptor {
    //被代理类对象引用
    private T target;
    public Object getInstance(T target){
        //被代理对象赋值
        this.target = target;
        //1.创建类加载器,用于生成被代理类
        Enhancer enhancer = new Enhancer();
        //2.指定被代理类的类型
        enhancer.setSuperclass(this.target.getClass());
        //3.设置回调
        enhancer.setCallback(this);
        //4.创建代理对象
        return enhancer.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        TestFunctionSpendTime.start = System.currentTimeMillis();
        System.out.println("谈论价钱");
        methodProxy.invokeSuper(o,args);
        System.out.println("交接钥匙");
        TestFunctionSpendTime.end = System.currentTimeMillis();
        TestFunctionSpendTime.display();
        return null;
    }
}
